import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AnimationTestRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AnimationTestRoute();
  }
}

class _AnimationTestRoute extends State<AnimationTestRoute>
    with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  Animation<double> _animation2;
  AnimationController _animationController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _animationController =
        new AnimationController(vsync: this, duration: Duration(seconds: 2));
    _animation =
        CurvedAnimation(parent: _animationController, curve: Curves.easeInCirc);
    _animation2 = CurvedAnimation(parent: _animationController, curve: Curves.decelerate);
//    _animation = new Tween(begin: 0.0, end: 1.0).animate(_animation)..addListener((){
//      setState(() {
//
//      });
//    });
    _animation.addStatusListener((state) {
      switch(state) {
        case AnimationStatus.completed:
          _animationController.reverse();
          break;
        case AnimationStatus.dismissed:
          _animationController.forward();
          break;
        case AnimationStatus.forward:
          break;
        case AnimationStatus.reverse:
          break;
      }
    });
    _animationController.forward();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var _width = MediaQuery.of(context).size.width;
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('动画测试'),
      ),
      body: Column(
        children: <Widget>[
//          ClipRect(
//            clipper: MyClipper(_width * _animation.value),
//            child: Image.asset('dsc03576.jpg',width: _width,),
//          ),
          AnimationImagesWidget(
            _width,
            animation: _animation,
          ),
          AnimatedBuilder(
            animation: _animation2,
            builder: (context, child) {
              return ClipRect(
                clipper: MyClipper(_width * _animation2.value),
                child: Image.asset('dsc03576.jpg',width: _width,),
              );
            },
          ),
          InkWell(
            onTap: () {
              // 渐变页面挑战动画
              Navigator.push(context, PageRouteBuilder(
                pageBuilder: (context, animation, secondAnimation) {
                  return FadeTransition(
                    opacity: animation,
                    child: HeroRoute(),
                  );
                }
              ));
//            Navigator.push(context, MaterialPageRoute(builder: (context) {
//              return HeroRoute();
//            }));
            },
            child: Hero(
              tag: "title",
              child: ClipOval(
                child: Image.asset('title.png', width: 80,),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class AnimationImagesWidget extends AnimatedWidget {
  final double _width;
  AnimationImagesWidget(this._width,{Animation<double> animation})
      : super(listenable: animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    // TODO: implement build
    return ClipRect(
            clipper: MyClipper(_width * animation.value),
            child: Image.asset('dsc03576.jpg',width: _width,),
          );
  }
}

class MyClipper extends CustomClipper<Rect> {
  var _width;

  MyClipper(this._width);

  @override
  Rect getClip(Size size) {
    // TODO: implement getClip
    return Rect.fromLTWH(0, 0, _width, size.height);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    // TODO: implement shouldReclip
    return true;
  }
}

class HeroRoute extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('Hero动画测试'),
      ),
      body: Center(
        child: Hero(tag: "title", child: Image.asset('title.png')),
      ),
    );
  }

}
